// TestAX.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "TestAX.h"
#include "TestAXDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestAXApp
#ifdef _DEBUG
typedef unsigned long ULONG_PTR;
typedef unsigned long *PULONG_PTR;
#include <dbghelp.h>
#pragma comment(lib, "Dbghelp.lib")

LONG WINAPI MsUnhandledFilter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
	LONG ret = EXCEPTION_EXECUTE_HANDLER;

	char szFileName[256]={0};
	char szExe[256]={0};
	SYSTEMTIME st;
	::GetLocalTime(&st);
	if(GetModuleFileNameA(NULL, szFileName, MAX_PATH ))
	{			
		char drive[_MAX_DRIVE];
		char dir[_MAX_DIR];
		char fname[_MAX_FNAME];
		char ext[_MAX_EXT];
		_splitpath( szFileName, drive, dir, fname, ext );
		strcpy(szExe,szFileName);
		sprintf(szFileName, "%s%s%s%04d%02d%02d-%02d%02d%02d.dmp", drive,dir,"testax_",st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
	}
	else
	{
		sprintf(szFileName, "%s%04d%02d%02d-%02d%02d%02d.dmp", "testax_",st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
	}

	HANDLE hFile = ::CreateFileA(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
	if (hFile != INVALID_HANDLE_VALUE)
	{
	   MINIDUMP_EXCEPTION_INFORMATION ExInfo;

	   ExInfo.ThreadId = ::GetCurrentThreadId();
	   ExInfo.ExceptionPointers = lpExceptionInfo;
	   ExInfo.ClientPointers = false;

	   BOOL bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL );

	   if (bOK)
	   {
			printf("Create Dump File=%s Success!\n",szFileName);
	   }
	   else
	   {
			printf("MiniDumpWriteDump Failed: %d\n", GetLastError());
	   }

	   ::CloseHandle(hFile);
	}
	else
	{
        printf("Create File %s Failed %d\n", szFileName, GetLastError());
	}
	return ret;
}

#endif

BEGIN_MESSAGE_MAP(CTestAXApp, CWinApp)
	//{{AFX_MSG_MAP(CTestAXApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestAXApp construction

CTestAXApp::CTestAXApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTestAXApp object

CTestAXApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CTestAXApp initialization

BOOL CTestAXApp::InitInstance()
{
	AfxEnableControlContainer();

#ifdef _DEBUG
		SetUnhandledExceptionFilter(MsUnhandledFilter);
#endif

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CTestAXDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
